package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.wb.dao.WbDevicesTypeDao;
import com.cyt.work.wb.model.po.WbDevicesType;
import com.cyt.work.wb.model.po.WxWbBulid;
import com.cyt.work.wb.model.vo.WbDevicesTypeVo;
import com.cyt.work.wb.model.vo.WxWbBulidVo;
import com.cyt.work.wb.service.WbDevicesTypeService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 维保模块-设备分类表Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:27:02
 */
@Service("wbDevicesTypeService")
public class WbDevicesTypeServiceImpl extends BaseServiceImpl<WbDevicesType> implements WbDevicesTypeService {

    @Resource
    private WbDevicesTypeDao wbDevicesTypeDao;

    @Autowired
    public void setBaseDao(WbDevicesTypeDao wbDevicesTypeDao) {
        this.baseDao = wbDevicesTypeDao;
    }

    /**
     * 查询对象集合
     *
     * @param wbDevicesTypeVo 对象
     * @return List 对象集合
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:02
     */
    @Override
    public List<WbDevicesType> listExample(WbDevicesTypeVo wbDevicesTypeVo) {
        Example example = new Example(WbDevicesType.class);
        example.setOrderByClause(" sort asc");
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbDevicesTypeVo.getName())) {
            criteria.andEqualTo("name",wbDevicesTypeVo.getName());
        }
        if (StringUtils.isNotBlank(wbDevicesTypeVo.getApplyId())) {
            criteria.andEqualTo("applyId",wbDevicesTypeVo.getApplyId());
        }
        if (StringUtils.isNotBlank(wbDevicesTypeVo.getParentId())) {
            criteria.andEqualTo("parentId",wbDevicesTypeVo.getParentId());
        }
        if (StringUtils.isNotBlank(wbDevicesTypeVo.getParentIds())) {
            criteria.andLike("parentIds",wbDevicesTypeVo.getParentIds());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wbDevicesTypeDao.selectByExample(example);
    }

    @Override
    public  List<WbDevicesTypeVo> selectByCondition(WbDevicesTypeVo wbDevicesTypeVo){
        return wbDevicesTypeDao.selectByCondition(wbDevicesTypeVo);
    }

    /**
     * 分页查询
     *
     * @param wbDevicesTypeVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:02
     */
    @Override
    public ReturnResult page(WbDevicesTypeVo wbDevicesTypeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbDevicesType> list = listExample(wbDevicesTypeVo);
        PageInfo<WbDevicesType> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbDevicesTypeVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbDevicesTypeVo wbDevicesTypeVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbDevicesTypeVo.getId())) {
            WbDevicesType wbDevicesType = new WbDevicesType(sysUser);
            wbDevicesType.setApplyId(wbDevicesTypeVo.getApplyId());
            wbDevicesType.setName(wbDevicesTypeVo.getName());
            wbDevicesType.setRemarks(wbDevicesTypeVo.getRemarks());
            wbDevicesType.setParentId(wbDevicesTypeVo.getParentId());
            if(org.apache.commons.lang3.StringUtils.isBlank(wbDevicesTypeVo.getParentId())){
                wbDevicesType.setParentIds("0");
                wbDevicesType.setExcelModel(wbDevicesTypeVo.getName());
            }else{
                WbDevicesType wbDevicesTypeP=wbDevicesTypeDao.selectByPrimaryKey(wbDevicesTypeVo.getParentId());
                wbDevicesType.setParentIds(wbDevicesTypeP.getParentIds()+","+wbDevicesTypeVo.getParentId());
                wbDevicesType.setExcelModel(wbDevicesTypeP.getExcelModel()+"-"+wbDevicesTypeVo.getName());
            }
            wbDevicesType.setSort(Integer.parseInt(wbDevicesTypeVo.getSort()));
            wbDevicesTypeDao.insert(wbDevicesType);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbDevicesType wbDevicesType = wbDevicesTypeDao.selectByPrimaryKey(wbDevicesTypeVo.getId());
            String oldParentIds=wbDevicesType.getParentIds();
            String oldExcelModel=wbDevicesType.getExcelModel();
            wbDevicesType.setApplyId(wbDevicesTypeVo.getApplyId());
            wbDevicesType.setName(wbDevicesTypeVo.getName());
            wbDevicesType.setRemarks(wbDevicesTypeVo.getRemarks());
            wbDevicesType.setParentId(wbDevicesTypeVo.getParentId());
            if(org.apache.commons.lang3.StringUtils.isBlank(wbDevicesTypeVo.getParentId())){
                wbDevicesType.setParentIds("0");
                wbDevicesType.setExcelModel(wbDevicesTypeVo.getName());
            }else{
                WbDevicesType wbDevicesTypeP=wbDevicesTypeDao.selectByPrimaryKey(wbDevicesTypeVo.getParentId());
                wbDevicesType.setParentIds(wbDevicesTypeP.getParentIds()+","+wbDevicesTypeVo.getParentId());
                wbDevicesType.setExcelModel(wbDevicesTypeP.getExcelModel()+"-"+wbDevicesTypeVo.getName());
            }
            wbDevicesType.setSort(Integer.parseInt(wbDevicesTypeVo.getSort()));
            wbDevicesType.setUpdateTime(new Date());
            wbDevicesType.setUpdateUser(sysUser.getId());
            wbDevicesTypeDao.updateByPrimaryKey(wbDevicesType);

            //批处理子目录
            //批处理子目录
            WbDevicesTypeVo wbDevicesTypeVocs = new WbDevicesTypeVo();
            wbDevicesTypeVocs.setParentIds("%"+wbDevicesType.getId()+"%");
            List<WbDevicesType> list = this.listExample(wbDevicesTypeVocs);
            for (WbDevicesType e : list){
                e.setParentIds(e.getParentIds().replace(oldParentIds, wbDevicesType.getParentIds()));
                e.setExcelModel(e.getExcelModel().replace(oldExcelModel,wbDevicesType.getExcelModel()));
                wbDevicesTypeDao.updateByPrimaryKey(e);
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbDevicesTypeVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbDevicesTypeVo wbDevicesTypeVo, SysUser sysUser) {
        WbDevicesTypeVo wbDevicesTypeVo1=new WbDevicesTypeVo();
        wbDevicesTypeVo1.setParentId(wbDevicesTypeVo.getId());
        List<WbDevicesType> wbDevicesTypeList=listExample(wbDevicesTypeVo1);
        if(wbDevicesTypeList.size()>0){
            return new ReturnResult(ErrorCode.CODE_1, BaseConstant.BASE_DEL_RULE_ERROR);
        }
        //要修改的字段
        WbDevicesType wbDevicesType = new WbDevicesType();
        wbDevicesType.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbDevicesType.setUpdateUser(sysUser.getId());
        wbDevicesType.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbDevicesType.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbDevicesTypeVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbDevicesTypeVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        int i=  wbDevicesTypeDao.updateByExampleSelective(wbDevicesType, example);
        if(i>0){
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_DEL);
        }else{
            return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_DEL);
        }
    }

    /**
     * 根据条件物理删除
     *
     * @param wbDevicesTypeVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbDevicesTypeVo wbDevicesTypeVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbDevicesType.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbDevicesTypeVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbDevicesTypeVo.getId().split(",")));
        }
        wbDevicesTypeDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 获取建筑物树（bulidClass=3 为建筑物 不能作为父类进行选择，屏蔽）
     *
     * @param wbDevicesTypeVo 参数接受对象
     * @return ReturnResult
     * @author dingxu
     * @date 2019-8-15 17:15
     */
    @Override
    public ReturnResult dTreeList(WbDevicesTypeVo wbDevicesTypeVo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<WbDevicesTypeVo> wbDevicesTypeList = this.selectByCondition(wbDevicesTypeVo);
        for (WbDevicesTypeVo wbDevicesType : wbDevicesTypeList) {
            Map<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("id", wbDevicesType.getId());
            resultMap.put("title", wbDevicesType.getName());
            resultMap.put("parentId", org.apache.commons.lang3.StringUtils.isBlank(wbDevicesType.getParentId()) ? "0" : wbDevicesType.getParentId());
            Map<String, String> map = new LinkedHashMap<>();
            resultMap.put("isLast",wbDevicesType.getCountChild().equals("0"));
            map.put("type", "0");
            map.put("isChecked", "0");
            List<Map<String, String>> checkArrList = new ArrayList<>();
            checkArrList.add(map);
            resultMap.put("checkArr", checkArrList);
            resultList.add(resultMap);
        }
        return new ReturnResult(resultList);
    }
}